package mdm.led.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import mdm.led.dao.UserMapper;
import mdm.led.entity.User;
import mdm.led.entity.UserApp;
import mdm.led.entity.UserAppPassword;
import mdm.led.service.UserService;
import mdm.led.util.DateUtil;
import mdm.led.util.MD5;

@Service("UserService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Transactional
	@Override
	public void add(User user) throws Exception {
		user.setCreateTime(DateUtil.formatDateTime(new Date()));
		short state = 1;
		short status = 1;
		user.setState(state);
		user.setStatus(status);
		user.setcPassword(MD5.getStandardMd5(user.getcPassword()));
		userMapper.insertSelective(user);
	}

	@Override
	public User selectByUsername(String username) throws Exception {
		return userMapper.selectByUsername(username);
	}

	@Override
	public List<User> selectByCondition(User user) throws Exception {
		List<User> list = new ArrayList<>();
		List<User> selectByCondition = userMapper.selectByCondition(user);
		if (user.getcJobnumber() != null && !"".equals(user.getcJobnumber())) {
			for (User item : selectByCondition) {
				if (!item.getcJobnumber().trim().equals(user.getcJobnumber().trim())) {
					continue;
				}
				list.add(item);
			}
		}else{
			list = selectByCondition;
		}
		return list;
	}

	@Override
	public User selectByRolePermission(Integer cUserId) {
		User user = userMapper.selectById(cUserId);
		return user;
	}

	@Override
	public void modify(User user) throws Exception {
		user.setUpdateTime(DateUtil.formatDateTime(new Date()));
		if (user.getcPassword() != null && !user.getcPassword().equals("")) {
			user.setcPassword(MD5.getStandardMd5(user.getcPassword()));
		}
		userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public void addApp(UserApp user) throws Exception {
		user.setCreateTime(DateUtil.formatDateTime(new Date()));
		short state = 1;
		user.setState(state);
		user.setRoleId(0);
		user.setcPassword(MD5.getStandardMd5(user.getcPassword()));
		userMapper.insertSelectiveApp(user);
	}

	@Override
	public void modifyApp(UserAppPassword user) throws Exception {
		User users = new User();
		users.setcUserId(user.getcUserId());
		users.setcUsername(user.getcUsername());
		if (user.getcPassword() != null && !user.getcPassword().equals("")) {
			users.setcPassword(MD5.getStandardMd5(user.getcPassword()));
		}
		userMapper.updateByPrimaryKeySelective(users);
	}

	@Override
	public User selectById(Integer cUserId) {
		return userMapper.selectById(cUserId);
	}

	@Override
	public List<User> selectByRoleId(Integer cUserId) {
		return userMapper.selectByRoleId(cUserId);
	}

	@Override
	public Integer updateState(Integer userID, Integer state) {
		return userMapper.updateState(userID, state);
	}

	@Override
	public List<User> findDepartmentUserByID(Integer departmentID, String jobnumber) {
		return userMapper.findResponsiblePolice(departmentID, jobnumber);
	}

	// @Override
	// public void modifyStatus(User user) throws Exception {
	// userMapper.updateByPrimaryKeySelective(user);
	// }

}
